<template>
    <div class="container">
        <div class='u-title f-mtp10'>
            <h2>{{h2_title}}</h2>
        </div>

        <div class='u-content mn flex flex-column flex-center'>
            <div class='item'>
                <label class='tip' for="">广告名称：</label>
                <div class='item-right'>
                    <el-input v-model="title" placeholder="广告名称"></el-input>
                    <p class='tip'>广告名称只是作为辨别多个广告条目之用，并不显示在广告中</p>
                </div>
            </div>
            <div class='item'>
                <label class='tip' for="">广告位置：</label>
                <div class='item-right'>
                    <el-select class='f-w-full' v-model="bannerType" clearable placeholder="请选择">
                        <el-option
                        v-for="item in bannerTypeList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class='item'>
                <label for="">上线/下线：</label>
                <div class='item-right radio-box'>
                  <el-radio v-model="state" label="normal">上线</el-radio>
                  <el-radio v-model="state" label="expired">下线</el-radio>
                </div>
            </div>
            <div class='item'>
                <label class='tip' for="">广告图片：</label>
                <div class='item-right'>
                    <div class="u-upload">
                      <input
                        type="file"
                        @change="upfile"
                        name="file"
                        id="file"
                        accept="image/png, image/jpeg, image/gif, image/jpg"
                        class="u-upload-file"
                      >
                      <label for="file" class="btn btn-success">
                        <img v-if="pic" :src="pic" alt="">
                        <i class='el-icon-plus' v-else></i>
                      </label>
                    </div>
                    <p class='tip'>只能上传jpg/png格式文件，文件不能超过50kb</p>
                </div>
            </div>
            <div class='item'>
                <label for="">广告链接：</label>
                <div class='item-right'>
                    <el-input v-model="link" placeholder="链接地址"></el-input>
                </div>
            </div>
            <div class='btn-box f-ptp20'>
                <el-button
                    class='f-w300 s-bg-green s-cfff'
                    @click="submit">提交</el-button>
            </div>
        </div>
    </div>
</template>

<script>
export default {
  name: 'AddAd',
  data() {
    return {
      h2_title: '添加广告',
      id: null,
      title: '',
      pic: '',
      state: 'normal',
      bannerType: '',
      link: '',
      bannerTypeList: [
        {
          value: 'pc',
          label: 'pc端'
        },
        {
          value: 'app',
          label: 'app端'
        },
        {
          value: 'example',
          label: '案例'
        },
        {
          value: 'video',
          label: '短剧'
        },
        {
          value: 'about',
          label: '关于我们'
        },
        {
          value: 'expert',
          label: '专家'
        }
      ],  
    }
  },
  computed: {
  
  },
  created: function() {
    if (this.$route.params.id) {
      this.id = this.$route.params.id;
      this.h2_title = "编辑广告";
      this.getAdDetailById(this.id);
    }
  },
  methods: {
    // 获取广告详情
    getAdDetailById(adId) {
      this.$store.dispatch('GET_BANNER_DETAIL', {
        id: adId
      }).then(res => {
        if(res.data.retCode == 0) {
          this.title = res.data.data.title;
          this.pic = res.data.data.pic;
          this.state = res.data.data.state;
          this.link = res.data.data.link;
          this.bannerType = res.data.data.type;
        }
      })
    },
    // 提交按钮点击事件
    submit() {
      if(this._utils.isEmpty(this.title)) {
        this.$message.warning('请输入广告名称！');
        return;
      }
      if(this._utils.isEmpty(this.bannerType)) {
        this.$message.warning('请选择广告位置！');
        return;
      }
      if(this._utils.isEmpty(this.pic)) {
        this.$message.warning('请上传广告图片！');
        return;
      }
      this.updateBanner();
    },  
    // 上传图片
    upfile(e) {
      const files = e.target.files;
      console.log("files:::", files);
      console.log("files[0]:::", files[0]);
      const form = new FormData();
      form.append("file", files[0]);
      form.append("dir", "banner");
      form.append("type", "1");
   
      this.$store.dispatch("UPLOAD_FILE", form).then(res => {
        console.log("res::::up:::", res);
        if(res.data.retCode == 0) {
          this.$message.success('上传成功！');
          this.pic = res.data.data;
        } else {
          res.data.errorMsg && this.$message.warning(res.data.errorMsg);
        }
      });
    },
    // banner管理-新建/更新
    updateBanner() {
      this.$store.dispatch('UPDATE_BANNER', {
        id: this.id,
        title: this.title,
        link: this.link,
        pic: this.pic,
        type: this.bannerType,
        state: this.state
      }).then(res => {
        if(res.data.retCode == 0) {
          this.$message.success('提交成功！');
          this.$router.push('/home/banner');
        } else {
          res.data.errorMsg && this.$message.warning(res.data.errorMsg);
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.radio-box{
  line-height: 40px;
}
.container{
  width: 800px;
  /* margin: 0 auto; */
}
.mn{
    padding: 30px 0 50px 0;
}
.item{
    display: flex;
    margin-bottom: 10px;
}
.item>label{
    display: inline-block;
    width: 90px;
    line-height: 40px;
    text-align: right;
    margin-right: 10px;
}
.item>.tip::before{
    content: '*';
    line-height: 40px;
    color: red;
    margin-right: 5px;
}
.item-right{
    width: 600px
}
.btn-box{
    padding-left: 90px;
}
.tip{
    font-size: 12px;
    color: #999;
    margin-bottom: 10px;
    margin-top: 10px;
    /* line-height: 24px; */
}
</style>
